﻿var sql = require( 'node-sqlserver' );
var path = require( 'path' );
var context = require( path.join( __dirname, "hrContext" ) );

exports.InsertTask = function ( task, successCall ) {

    context.openHRConn( function ( conn ) {

        conn.query(
            "insert into task(ApplicationID,BusinessTypeID,workflowID,workflowProcessID,Node,ApprocedEmpID,workflowStartTime,OperatorID,GroupCode) values(?,?,?,?,?,?,?,?,?)",
            [
                task.ApplicationID,
                task.BusinessTypeID,
                task.workFlowID,
                task.workFlowProcessId,
                task.Node,
                task.ApprocedEmpID,
                new Date(),
                task.OperatorID,                task.GroupCode
            ],
            function ( err, rows ) {
                if ( err ) {
                    err = "InsertTask:" + err;
                    console.log( "Select in DB Error: " + err );
                }

                if ( successCall && typeof successCall == "function" ) { successCall( err, rows ); }

            });
    });

};

exports.ClearTaskByApplicationID = function( applicationID , successCall){
    
    context.openHRConn( function ( conn ) {
        
        conn.query("delete from task where applicationid = ?",[ applicationID ],function( err, result){
            if ( err ) {
                err = "ClearTaskByApplicationID:" + err;
                console.log( "Select in DB Error: " + err );
            }

            if ( successCall && typeof successCall == "function" ) { successCall(  err, result ); }
        });
            
    });
        
};

exports.UpdateTaskOperatorID = function( applicationID, operatorID, successCall){
    
    context.openHRConn( function ( conn ) {
        
        operatorID = operatorID || '';

        conn.query(
            "update task set operatorid = ? where applicationid = ? and operatorid = 0",
            [ operatorID, applicationID ],
            function( err, result ){
                if ( err ) {

                    err = "UpdateTaskOperatorID:" + err;
                    console.log( "Select in DB Error: " + err );
                    
                }

                if ( successCall && typeof successCall == "function" ) { successCall(  err, result ); }
            });
            
    });

};

exports.FindNewestTaskByApplicationID = function( applicationID , successCall){
    
    context.openHRConn( function ( conn ) {
        
        conn.query( 
            "select * from Task where operatorid = 0 and applicationid = ?", 
            [ applicationID ],
            function( err, rows ){
                
                if( err ){
                    err = "FindNewestTaskByApplicationID:" + err;
                    console.log( "Select in DB Error: " + err );  
                }

                var result = null;

                if(rows && rows.length > 0) result = rows[0];

                if ( successCall && typeof successCall == "function" ) { successCall(  err, result ); }

            });

    });

};